/*
 * @Author: fangzhi 
 * @Date: 2018-09-18 09:42:43 
 * @Last Modified by: fangzhioo
 * @Last Modified time: 2018-12-25 16:19:38
 */
 /**
 * 初始化jsbridge
 * @param readyCallback 初始化完成后的回调
 */
function initJsBridge(readyCallback) {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    // 注册jsbridge
    function connectWebViewJavascriptBridge(callback) {
        if (isAndroid) {
            if (window.WebViewJavascriptBridge) {
                callback(WebViewJavascriptBridge)
            } else {
                document.addEventListener(
                    'WebViewJavascriptBridgeReady'
                    , function () {
                        callback(WebViewJavascriptBridge)
                    },
                    false
                );
            }
            return;
        }
        if (isiOS) {
            if (window.WebViewJavascriptBridge) {
                return callback(WebViewJavascriptBridge);
            }
            if (window.WVJBCallbacks) {
                return window.WVJBCallbacks.push(callback);
            }
            window.WVJBCallbacks = [callback];
            var WVJBIframe = document.createElement('iframe');
            WVJBIframe.style.display = 'none';
            WVJBIframe.src = 'https://__bridge_loaded__';
            document.documentElement.appendChild(WVJBIframe);
            setTimeout(function () {
                document.documentElement.removeChild(WVJBIframe)
            }, 0)
        }
    }
    // 调用注册方法
    connectWebViewJavascriptBridge(function (bridge) {
        if (isAndroid) {
            bridge.init(function (message, responseCallback) {
                console.log('JS got a message', message);
                responseCallback(data);
            });
        }
         
        // 只有在这里注册过的方法，在原声代码里才能用callHandler的方式调用
        bridge.registerHandler('backClick', function (data, responseCallback) {
            showResponse(data);
        });
        bridge.registerHandler('firstAuth', function (data, responseCallback) {
            showResponse(data);
        });
        bridge.registerHandler('backAddCard', function (data, responseCallback) {
            showResponse(data);
        });
        bridge.registerHandler('JSGetAddress', function (data, responseCallback) {
            showResponse(data);
        });
        if(readyCallback){
            readyCallback();
        }
         
    });
}

initJsBridge(()=>{
    console.log("注册js交互事件成功！");
});